package com.qding.park.manager.rest.conf;


import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import com.mongodb.Mongo;
import com.qding.park.common.core.redis.JedisClient;
import com.qding.park.common.core.redis.impl.JedisClientSingle;

import redis.clients.jedis.JedisPool;

/**
 * @author ly
 *mongo  config bean
 */
@Service
@Scope("singleton")
public class MongoDatasourceConfigBean {

	private static final Logger logger = LoggerFactory.getLogger(MongoDatasourceConfigBean.class);

	@Autowired
	private MongoDatasourceConfig mongoDatasourceConfig;

	@Bean(name = "mongo")
	public Mongo mongo() {
		logger.info("mongo=" + mongoDatasourceConfig);
		logger.info("mongoDatasourceConfig config ==> host:{}, port:{} ",mongoDatasourceConfig.getDatabaseName(),mongoDatasourceConfig.getHost(),mongoDatasourceConfig.getPort());
		// 加载配置文件属性
		Mongo mongo=new Mongo(mongoDatasourceConfig.getHost(),mongoDatasourceConfig.getPort());
		return mongo;
	}

	
	@Bean(name="mongoTemplate")
	public MongoTemplate mongoTemplate() throws RuntimeException {
		logger.info("--> mongoTemplate");
		MongoTemplate mongoTemplate=new MongoTemplate(mongo(),mongoDatasourceConfig.getDatabaseName());
		
		return mongoTemplate;
	}
	

    /**
      * postConstruct(这里用一句话描述这个方法的作用)
      * TODO(这里描述这个方法适用条件 – 可选)
      * TODO(这里描述这个方法的执行流程 – 可选)
      * TODO(这里描述这个方法的使用方法 – 可选)
      * TODO(这里描述这个方法的注意事项 – 可选)
      *
      * @Title: postConstruct
      * @Description:  通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
      * @param     设定文件
      * @return void    返回类型
      * @throws
      */
    @PostConstruct
    public void postConstruct() {
        logger.info("mongoDatasourceConfig={}", mongoDatasourceConfig);
    }
	
//	public Resource[] getResource(String basePackage, String pattern) throws IOException {
//		String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(new StandardEnvironment().resolveRequiredPlaceholders(basePackage)) + "/" + pattern;
//		Resource[] resources = new PathMatchingResourcePatternResolver().getResources(packageSearchPath);
//		return resources;
//	}
}
